/* eslint-disable */
import BASE_PRINT from '@/views/tools/print/basePrint'

// Lodop
const print = new BASE_PRINT('Lodop打印插件', '')

print.init = function() {
  const that = this
  return new Promise(function(resolve, reject) {
    if (needCLodop()) {
      loadCLodop() // 加载js
      getLodop().then(lodop => { // 获取lodop对象
        lodop.PRINT_INIT('蓝蓝订单')
        lodop.SET_PRINT_STYLE('FontSize', 18)
        lodop.SET_PRINT_STYLE('Bold', 1)
        lodop.ADD_PRINT_TEXT(300, 231, 260, 39, '打印页面部分内容')
        lodop.ADD_PRINT_HTM('3cm', '1cm', '8cm', '20cm', doc)
        that.socket = lodop
        that.connect = true
        that.msg = ''
        resolve && resolve(lodop)
      }).catch(msg => {
        that.msg = msg
        reject && reject(msg)
      })
    } else {
      let msg = '浏览器不支持Lodop插件'
      that.msg = msg
      console.error(msg)
      reject && reject(msg)
    }
  })
}

print.getPrinters = function() {
  const lodop = this.socket
  const printCount = lodop.GET_PRINTER_COUNT()
  const printers = []
  for (let i = 0; i < printCount; i++) {
    printers.push({ name: lodop.GET_PRINTER_NAME(i) })
  }
  return printers
}

print.print = function(data, printName) {
  this.socket.SET_PRINTER_INDEXA(printName);
  this.socket.PREVIEW()
}
let CreatedOKLodopObject, CLodopIsLocal, CLodopJsState

// ==判断是否需要CLodop(那些不支持插件的浏览器):==
function needCLodop() {
  try {
    let ua = navigator.userAgent
    if (ua.match(/Windows\sPhone/i)) { return true }
    if (ua.match(/iPhone|iPod|iPad/i)) { return true }
    if (ua.match(/Android/i)) { return true }
    if (ua.match(/Edge\D?\d+/i)) { return true }

    let verTrident = ua.match(/Trident\D?\d+/i)
    let verIE = ua.match(/MSIE\D?\d+/i)
    let verOPR = ua.match(/OPR\D?\d+/i)
    let verFF = ua.match(/Firefox\D?\d+/i)
    let x64 = ua.match(/x64/i)
    if ((!verTrident) && (!verIE) && (x64)) { return true } else if (verFF) {
      verFF = verFF[0].match(/\d+/)
      if ((verFF[0] >= 41) || (x64)) { return true }
    } else if (verOPR) {
      verOPR = verOPR[0].match(/\d+/)
      if (verOPR[0] >= 32) { return true }
    } else if ((!verTrident) && (!verIE)) {
      let verChrome = ua.match(/Chrome\D?\d+/i)
      if (verChrome) {
        verChrome = verChrome[0].match(/\d+/)
        if (verChrome[0] >= 41) { return true }
      }
    }
    return false
  } catch (err) {
    return true
  }
}

// ==加载引用CLodop的主JS,用双端口8000和18000(以防其中一个被占):==
function loadCLodop() {
  if (CLodopJsState === 'loading' || CLodopJsState === 'complete') return
  CLodopJsState = 'loading'
  let head = document.head || document.getElementsByTagName('head')[0] || document.documentElement
  let JS1 = document.createElement('script')
  let JS2 = document.createElement('script')
  JS1.src = 'http://localhost:8000/CLodopfuncs.js?priority=1'
  JS2.src = 'http://localhost:18000/CLodopfuncs.js'
  JS1.onload = JS2.onload = function() { CLodopJsState = 'complete' }
  JS1.onerror = JS2.onerror = function(evt) { CLodopJsState = 'complete' }
  head.insertBefore(JS1, head.firstChild)
  head.insertBefore(JS2, head.firstChild)
  CLodopIsLocal = !!((JS1.src + JS2.src).match(/\/\/localho|\/\/127.0.0./i))
}

// ==获取LODOP对象主过程,判断是否安装、需否升级:==
function getLodop(oOBJECT, oEMBED) {
  return new Promise(function(resolve, reject) {
    const strHtmInstall = "打印控件未安装!点击这里<a href='javascript:;' class='donwLink' filename='CLodop.exe'>下载执行安装</a>,安装后请刷新页面或重新进入。"
    const strHtmUpdate = "打印控件需要升级!点击这里<a href='javascript:;' class='donwLink' filename='CLodop.exe'>执行升级</a>,升级后请重新进入。"
    const strHtm64_Install = "打印控件未安装!点击这里<a href='javascript:;' class='donwLink' filename='CLodop.exe'>下载执行安装</a>,安装后请刷新页面或重新进入。"
    const strHtm64_Update = "打印控件需要升级!点击这里<a href='javascript:;' class='donwLink' filename='CLodop.exe'>执行升级</a>,升级后请重新进入。"
    const strHtmFireFox = "<br>（注意：如曾安装过Lodop旧版附件npActiveXPLugin,请在【工具】->【附加组件】->【扩展】中先卸它）"
    const strHtmChrome = "<br>(如果此前正常，仅因浏览器升级或重安装而出问题，需重新执行以上安装）"
    const strCLodopInstall_1 = "CLodop未安装启动，点击这里<a href='javascript:;' class='donwLink' filename='CLodop.exe'>下载执行安装</a>"
    const strCLodopInstall_2 = "<br>（若此前已安装过，可<a href='CLodop.protocol:setup' style='color:red;'>点这里直接再次启动</a>）"
    const strCLodopInstall_3 = '，成功后请刷新本页面。</font>'
    // let strCLodopUpdate = "<br><font color='#FF00FF'>Web打印服务CLodop需升级!点击这里<a href='/fore/img/utils/CLodop_Setup_for_Win32NT.exe' target='_self'>执行升级</a>,升级后请刷新页面。</font>"
    let LODOP
    try {
      let ua = navigator.userAgent
      let isIE = !!(ua.match(/MSIE/i)) || !!(ua.match(/Trident/i))
      if (needCLodop()) {
        try { LODOP = getCLodop() } catch (err) {}
        if (!LODOP && CLodopJsState !== 'complete') {
          setTimeout(function() {
            getLodop().then(lodop => {
              resolve && resolve(lodop)
            }).catch(msg => {
              reject && reject(msg)
            })
          }, 1000)
          return
        }
        if (!LODOP) {
          let msg = strCLodopInstall_1 + (CLodopIsLocal ? strCLodopInstall_2 : '') + strCLodopInstall_3
          reject && reject(msg)
          return
        } else {
          if (CLODOP.CVERSION < '4.1.0.4') {
            reject && reject(strCLodopUpdate)
          }
          if (oEMBED && oEMBED.parentNode) { oEMBED.parentNode.removeChild(oEMBED) } // 清理旧版无效元素
          if (oOBJECT && oOBJECT.parentNode) { oOBJECT.parentNode.removeChild(oOBJECT) }
        }
      } else {
        let is64IE = isIE && !!(ua.match(/x64/i))
        // ==如果页面有Lodop就直接使用,否则新建:==
        if (oOBJECT || oEMBED) {
          if (isIE) { LODOP = oOBJECT } else { LODOP = oEMBED }
        } else if (!CreatedOKLodopObject) {
          LODOP = document.createElement('object')
          LODOP.setAttribute('width', 0)
          LODOP.setAttribute('height', 0)
          LODOP.setAttribute('style', 'position:absolute;left:0px;top:-100px;width:0px;height:0px;')
          if (isIE) { LODOP.setAttribute('classid', 'clsid:2105C259-1E0C-4534-8141-A753534CB4CA') } else { LODOP.setAttribute('type', 'application/x-print-lodop') }
          document.documentElement.appendChild(LODOP)
          CreatedOKLodopObject = LODOP
        } else { LODOP = CreatedOKLodopObject }
        // ==Lodop插件未安装时提示下载地址:==
        if ((!LODOP) || (!LODOP.VERSION)) {
          let msg = ''
          if (ua.indexOf('Chrome') >= 0) { msg = strHtmChrome }
          if (ua.indexOf('Firefox') >= 0) { msg = strHtmFireFox }
          reject && reject(msg + (is64IE ? strHtm64_Install : strHtmInstall))
          return
        }
      }
      if (LODOP.VERSION < '6.2.2.6') {
        reject && reject(is64IE ? strHtm64_Update : strHtmUpdate)
      }
      // ===如下空白位置适合调用统一功能(如注册语句、语言选择等):==

      LODOP.SET_LICENSES('', 'E07F78ADBA49291D8FC873495465728C949', '23580B189D96F33252722697A57AC00506B', '')
      // =======================================================
      resolve && resolve(LODOP)
      return LODOP
    } catch (err) {
      reject && reject('getLodop出错:' + err)
    }
  })
}

let doc = `
<!DOCTYPE html>
<html>
<head>
	<title></title>
	<style type="text/css">
		.awb{
			width: 8cm;
			padding-left: 1cm;
			padding-right: 1cm;
			font-family: "Arial";
		}
		.awb>div{
			border-bottom: 1px solid gray;
		}
		[inline]{
			float: left;
		}
		[borderTop]{
			border-top: 0.1cm solid;
		}
		[borderBottom]{
			border-bottom: 0.1cm solid;
		}
		[outline]{
			outline: 1px solid gray;
		}
	</style>
</head>
<body>
<div class="awb">
	<div style="height: 6cm;">
		<div  style="width: 8.0cm">
			<img src="./head.png" style="width:7cm;height: 2cm;transcolor='#FFFFFF'" />
		</div>
		<div inline style="width: 4.4cm; height: 0.9cm;"></div>
		<div inline style="width: 2.4cm; font-size: 12pt;text-align: right;">代收货款</div>
	</div>
	<div style="height: 1.5cm;font-weight: bold;font-size: 32pt;">
		016D-110-211
	</div>
	<div style="height: 1.0cm;">
		<div inline style="width: 7.4cm;font-size: 20pt;">湖南省海口市</div>
		<div inline style="width: 2.4cm;font-size: 6pt;">集包地一维码</div>
	</div>
	<div style="height: 3.0cm;">
		<div inline style="width: 7.4cm;">
			<div inline outline style="width: 0.5cm;height: 1.8cm;font-size: 8pt;
    				line-height: 0.9cm;">收件</div>
			<div inline outline style="width: 6.9cm;height: 1.8cm;font-size: 8pt;">
				&nbsp;&nbsp;张伟  123154564  132123131<br>
				&nbsp;&nbsp;湖南省海口市......
			</div>
			<div inline outline style="width: 0.5cm;height: 1.2cm;    font-size: 8pt;
	    			line-height: 0.6cm;">寄件</div>
			<div inline outline style="width: 6.9cm;height: 1.2cm;font-size: 6pt;">
				&nbsp;&nbsp;张伟  123154564  132123131<br>
				&nbsp;&nbsp;湖南省海口市......
			</div>
		</div>
		<div inline style="width: 2.6cm;">
			<div inline style="width: 2.4cm;height: 0.5cm;font-size: 6pt;text-align: center;margin-left: 1px;">服务</div>
			<div inline style="width: 2.4cm;height: 2.5cm"></div>
		</div>
	</div>
	<div style="height: 2.5cm">
		面单号及面单号一维码
	</div>
	<div style="height: 1.9cm;" borderBottom>
		<div inline outline style="width: 4.4cm;height: 1.9cm;font-size: 6pt;"></div>
		<div inline outline style="width: 3.5cm; font-size: 6pt;height: 1.9cm;margin-left: 1px;">
			&nbsp;&nbsp;签收人：<br>
			&nbsp;&nbsp;时间：
		</div>
		<div inline style="width: 1.9cm">快递公司二维码</div>
	</div>
	<div style="height: 1.4cm;" borderTop>
		<div inline style="width: 3.0cm">公司logo</div>
		<div inline style="width: 6.8cm">面单号一维码</div>
	</div>
	<div style="height: 2.4cm;">
		<di inline style="height: 2.4cm;width: 7.9cm;">
			<div inline outline style="width: 0.5cm;height: 1.2cm;line-height: 0.6cm;font-size: 8pt;">收件</div>
			<div inline outline style="width: 7.4cm;height: 1.2cm;font-size: 6pt;">
				&nbsp;&nbsp;张伟  123154564  132123131<br>
				&nbsp;&nbsp;湖南省海口市......
			</div>

			<div inline outline style="width: 0.5cm;height: 1.2cm;line-height: 0.6cm;font-size: 8pt;">寄件</div>
			<div inline outline style="width: 7.4cm;height: 1.2cm;font-size: 6pt;">
				&nbsp;&nbsp;张伟  123154564  132123131<br>
				&nbsp;&nbsp;湖南省海口市......
			</div>
		</di>


		<div inline style="width: 1.9cm;height: 2.4cm;">二维码</div>
	</div>
	<div style="height: 3cm;" borderBottom>
	</div>
</div>
</body>
</html>
`
export default print
